define(function(require,exports,module){
	var emailContent = '';
	var toAddress = '';
	var personal = '';
	var subject = '';
	var folderType = '';
	var number = '';
	var fileItem = 1;
	var sendType ='';
	var pid = '';
	var clearFrom = true;
	var innerEmailList = {};
	var externalEmailList = {};
	var signHtml = '';
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var ajax = require("resource/js/component/ajax.js");
	var easyutil = require("resource/js/component/easyutil.js");
	require("resource/js/component/rules.js");
	
	require("resource/js/ueditor/umeditor.config.js");
	require("resource/js/ueditor/umeditor.js");
	require("resource/js/ueditor/themes/default/css/umeditor.css");
	require("resource/js/ueditor/jquery.ueditor.js");
	
	var urltool = require("resource/js/component/urlTool.js");
	
//	//---------------收件箱详情页参数--------------------------
//	emailContent = decodeURI(urltool.getParam("emailContent"));
//	toAddress = urltool.getParam("toAddress");
//	personal = decodeURI(urltool.getParam("personal"));
//	subject = decodeURI(urltool.getParam("subject"));
//	//---------------草稿箱详情页参数--------------------------
	folderType = urltool.getParam("folderType");
	number = urltool.getParam("number");
	sendType = urltool.getParam("sendType");
	if(window.sessionStorage){
		var oaEmailViewObj = JSON.parse(window.sessionStorage.getItem('oa_email_view_obj'));
		if (oaEmailViewObj && folderType != null && folderType != '') {
			emailContent = oaEmailViewObj.emailContent;
			toAddress = oaEmailViewObj.toAddress;
			personal = oaEmailViewObj.personal;
			subject = oaEmailViewObj.subject;
		}
	} else {
		alert('您的浏览器版本太低');
	}
	if (folderType != null && folderType != '') {
		$(".back").show();
	}
	$(".back").click(function() {
		if (folderType == 1) {
			window.location.href = BASEPATH + "/view/oa/email/viewEmail?pid="+pid+"&number="+number+"&folderType="+folderType;
		} else if (folderType == 2) {
			window.location.href = BASEPATH + "/view/oa/email/viewEmail?pid="+pid+"&number="+number+"&folderType="+folderType;
		} else if (folderType == 3) {
			window.location.href = BASEPATH + "/view/oa/email/draftEmail?pid="+pid;
		}
	});
	$('<script id="editor" type="text/plain" style="width:100%;height:300px;"></script>').insertAfter($("#emailText"));
	var ue = UM.getEditor('editor',{
		imageUrl:BASEPATH+"/resource/js/ueditor/jsp/imageUp.jsp",
		imagePath: BASEPATH + "/"
	});
	
	 //处理提交数据
	 var createFromData = function(url, sendType) {
		 var toAddressError = false;
		 var ccAddressError = false;
		 var bccAddressError = false;
		 var errorMsg = '';
		 
		 var fileList = [];
		 $(".file-new-list").each(function(i, n) {
			 var fileName = $(n).find('.file-finish-name').text();
			 var filePath = $(n).find('.filePath').val();
			 fileList[i] = {fileName: fileName, filePath: filePath}; 
		 });
		 
		 var oldAttachFiles = [];
		 $(".file-old-list").each(function(i, n) {
			 var fileName = $(n).find('.file-finish-name').text();
			 var fileId = $(n).find('.fileId').val();
			 oldAttachFiles[i] = {fileName: fileName, fileId: fileId}; 
		 });
		 var toAddressList = [];
		 $("#toAddr .e-email-block").each(function(i, n) {
			 if ($(n).data('e-error') == true || $(n).data('e-error') == 'true') {
				 toAddressError = true;
				 errorMsg = '收件人中有无效邮箱';
			 }
			 toAddressList[i] = {personal: $(n).data('e-name'), email: $(n).data('e-email')};
		 });
		 if (sendType == 'send') {
			 if (toAddressList.length <= 0) {
				 showErrorMessage("收件人不能为空");
				 return;
			 }
		 }
		 if (toAddressError) {
			 showErrorMessage(errorMsg);
			 return;
		 }
		 if (toAddressList.length > 100) {
			 showErrorMessage("收件人超过100人");
			 return;
		 }
		 var ccAddressList = [];
		 $("#toCC .e-email-block").each(function(i, n) {
			 if ($(n).data('e-error') == true || $(n).data('e-error') == 'true') {
				 ccAddressError = true;
				 errorMsg = '抄送人中有无效邮箱';
			 }
			 ccAddressList[i] = {personal: $(n).data('e-name'), email: $(n).data('e-email')};
		 });
		 
		 if (ccAddressError) {
			 showErrorMessage(errorMsg);
			 return;
		 }
		 if (ccAddressList.length > 100) {
			 showErrorMessage("抄送人超过100人");
			 return;
		 }
		 var bccAddressList = [];
		 $("#toBCC .e-email-block").each(function(i, n) {
			 if ($(n).data('e-error') == true || $(n).data('e-error') == 'true') {
				 bccAddressError = true;
				 errorMsg = '密送人中有无效邮箱';
			 }
			 bccAddressList[i] = {personal: $(n).data('e-name'), email: $(n).data('e-email')};
		 });
		 
		 if (bccAddressError) {
			 showErrorMessage(errorMsg);
			 return;
		 }
		 if (ccAddressList.length > 100) {
			 bccAddressList("密送人超过100人");
			 return;
		 }
		 if ($('#subject').val() == null || $('#subject').val() == "") {
			 showErrorMessage('主题不能为空');
			 return;
		 }
		 if ($('#subject').length > 500) {
			 showErrorMessage('主题文字超过500字符');
			 return;
		 }
		 var sendEmailContent = UM.getEditor('editor').getContent();
//		 if (sendType == 'send') {
		 //内容可以为空 5263
//			 if (sendEmailContent == null || sendEmailContent == "") {
//				 showErrorMessage('内容不能为空');
//				 return;
//			 }
//		 }
		 var param = {
			toAddressList: toAddressList,
			ccAddressList: ccAddressList,
			bccAddressList: bccAddressList,
			subject: $('#subject').val(),
			emailText: sendEmailContent,
			fileList: fileList,
			oldAttachFiles: oldAttachFiles,
			priority: $("#priority:checked").val(),
			replyTo: $("#replyTo:checked").val()
		 };
		 if (number != '' && number != null) {
			 param = $.extend(param, {number: number});
		 }
		 if (folderType != '' && folderType != null) {
			 param = $.extend(param, {folderType: folderType});
		 }
		 $("#sendLoading").show();
		 ajax.init().success(function(code,msg,data){
			 $("#sendLoading").hide();
			 if(code==0){
				 $.messager.show({
						title:'',
						msg:msg,
						timeout:1000,
						showType:'slide'
					});
//				 if (clearFrom) {
					 $('#sendEmailForm')[0].reset();
					 $("#toAddr .e-email-block").remove();
					 $("#toCC .e-email-block").remove();
					 $("#toBCC .e-email-block").remove();
					 UM.getEditor('editor').setContent(signHtml);
					 $('#fileFinish').html('');
//				 }
			 }
		 }).error(function(code,msg,data){
			 $("#sendLoading").hide();
			 $.messager.show({
				title:'',
				msg:msg,
				timeout:1000,
				showType:'slide'
			});
		 }).post(BASEPATH + url, param);
	 }
	 
	function showErrorMessage(msg) {
		 $.messager.show({
			 title:'',
			 msg: msg,
			 timeout:1000,
			 showType:'slide'
		 });
	}
	 
	//节码转换
	function bytesToSize(bytes) {
	    if (bytes === 0) {
	    	return '0 B';
	    }
	    var k = 1024,
	        sizes = ['B', 'KB', 'MB', 'GB'],
	        i = Math.floor(Math.log(bytes) / Math.log(k));
	   return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
	}
	//删除文件
	$("#fileFinish").on('click', '.file-finish-del', function() {
		var parentsDiv = $(this).parents('.file-finish-block');
		var filePath = parentsDiv.find('.filePath').val();
		if (filePath == '') {
			 parentsDiv.remove();
		} else {
			ajax.init().success(function(code,msg,data){
				if(code == 0){
					parentsDiv.remove();
				} else {
					$.messager.show({
						title:'',
						msg:msg,
						timeout:1000,
						showType:'slide'
					});
				}
			}).post(BASEPATH + "/oa/email/deleteAttachment.htm", {filePath: filePath});
		}
	});
	//页面跳转处理回填数据
	setTimeout(function() {
		if (toAddress != '' && toAddress != null && toAddress != 'null') {
			$("#toAddr .e-edit-addr").parent().before(addrHtmlTmp(personal, toAddress, false));
		}
		if (subject != '' && subject != null && subject != "null") {
			$('#subject').val(subject);
		}
		
		if (folderType != null && folderType != '' && number != null && number != '') {
			ajax.init().success(function(code,msg,data){
				if(code==0){
					if (folderType == 3 || sendType == 'forward') {//草稿箱详情
						if (sendType != 'forward') {
							emailListToForm($("#toAddr .e-edit-addr"), data.toAddressList);
							$('#subject').val(data.subject);
						}
						emailListToForm($("#toCC .e-edit-addr"), data.ccAddressList);
						emailListToForm($("#toBCC .e-edit-addr"), data.bccAddressList);
//						UM.getEditor('editor').setContent(data.emailText);
						for (var i = 0; i < data.fileList.length; i++) {
							var html='<div class="file-old-list file-finish-block file-item-'+fileItem+'">'
							+ '<div class="file-finish-name">'+data.fileList[i].fileName+'</div><div class="file-finish-del-block">'
							+ '<a href="javascript:void(0)" class="file-finish-del">删除</a></div>'
							+ '<div class="file-finish-result">'+bytesToSize(data.fileList[i].fileSize)+'<span class="file-finish-result-msg"></span></div>'
							+ '<div class="file-finish-bar-block"><div class="file-finish-bar"></div></div>'
							+ '<div class="file-finish-bar-result">0%</div>'
							+ '<input type="hidden" class="fileId" value="'+data.fileList[i].fileId+'"></div>'
							$("#fileFinish").append(html);
							$(".file-item-"+fileItem+" .file-finish-bar-result").remove();
							$(".file-item-"+fileItem+" .file-finish-bar").remove();
							$(".file-item-"+fileItem+" .file-finish-result").show();
							$(".file-item-"+fileItem+" .file-finish-bar-block").remove();
							$(".file-item-"+fileItem+" .file-finish-del-block").show();
							fileItem += 1;
						}
						if (data.replyTo) {
							$("#replyTo").attr("checked", true);
						}
						if (data.priority == "紧急") {
							$("#priority").attr("checked", true);
						} 
					}
					
					var historyContent = '';
					//查询签名
					ajax.init().success(function(code,msg,obj){
						 if(code==0){
							 if (emailContent != null && emailContent != undefined && emailContent != '' && emailContent != 'null') {
								 historyContent += emailContent;
							 }
							 if (obj) {
								 if (folderType != 3) {
									 if (obj.signContent != null && obj.signContent != '') {
										 signHtml = '<br><br><br><br><br>' + obj.signContent;
										 historyContent += signHtml;
									 }
								 }
							 }
							 
							 if (folderType != 3) {//sendType != 'forward' && 
								 var ccAddrHtml = '';
								 if (data.ccAddressList.length > 0) {
									 ccAddrHtml = '<div><b>抄送人:</b>&nbsp;'+parseEmailAddr(data.ccAddressList)+' <wbr></div><div></div>';
								 }
								 var timeStr = '';
								 if (folderType == 1) {
									 timeStr = data.receivedDate;
								 } else {
									 timeStr = data.sendDate;
								 }
								 historyContent += '<div><br></div><div><br></div>'
									 + '<div style="font-size: 12px;font-family: Arial Narrow;padding:2px 0 2px 0;">------------------&nbsp;原始邮件&nbsp;------------------</div>'
									 + '<div style="font-size: 12px;background:#efefef;padding:8px;">'
									 + '<div><b>发件人:</b>&nbsp;'+parseEmailAddr(data.fromAddressList)+'</div>'
									 + '<div><b>发送时间:</b>&nbsp;'+timeStr+'</div>'
									 + '<div><b>收件人:</b>&nbsp;'+parseEmailAddr(data.toAddressList)+' <wbr></div><div></div>'
									 + ccAddrHtml
									 + '<div><b>主题:</b>&nbsp;'+data.subject+'</div></div><div><br></div>';
								 
								 historyContent += data.emailText + '<div><br></div>';
							 }
							 if (folderType == 3) {//sendType == 'forward' || 
								 historyContent += data.emailText;
							 }
							 
							 UM.getEditor('editor').setContent(historyContent);
							 
						 }
					}).post(BASEPATH + "/oa/email/getEmailSign.htm")
					
				}
			}).post(BASEPATH + "/oa/email/getViewEmail.htm",{number: number,folderType: folderType},'get');
		} else {
			//进入发送查询签名
			ajax.init().success(function(code,msg,data){
				 if(code==0){
					 if (data) {
						 if (data.signContent != null && data.signContent != '') {
							 signHtml = '<br><br><br><br><br>' + data.signContent;
							 UM.getEditor('editor').setContent(signHtml);
						 }
					 }
				 }
			}).post(BASEPATH + "/oa/email/getEmailSign.htm")
		}
		
	}, 500);
	//邮件地址回填表单
	function emailListToForm(ele, emailList) {
		var addr = '';
		var name = '';
		var email = '';
		if(emailList){
			for (var i = 0; i < emailList.length; i++) {
				if (emailList[i].personal != null && emailList[i].personal != "") {
					name = emailList[i].personal
				} 
				if (emailList[i].email) {
					email = emailList[i].email;
				}
				addr += addrHtmlTmp(name, email, false);
			}
			ele.parent().before(addr);
		}
	}
	//解析邮件联系人
	function parseEmailAddr(addrList) {
		var addr = '';
		if(addrList){
			for (var i = 0; i < addrList.length; i++) {
				if (addrList[i].personal != null && addrList[i].personal != "") {
					addr += addrList[i].personal
				} 
				if (addrList[i].email) {
					addr += '&lt;' + addrList[i].email + '&gt;;';
				}
			}
		}
		return addr;
	}
	
	$(".e-edit-addr").keydown(function(event) { //文本框会随着字数的增多而自适应的增长
		var self = $(this);
		this.size = self.val().length + 2; 
		if (self.val() == "") {
			if (event.keyCode == 8) { //删除键
				if(self.parent().prev().hasClass('e-email-block')){
					self.parent().prev().remove();
				}
			}
		}
	}).blur(function(){ //失去焦点
		var self = $(this);
		var address = self.val().trim();
		if (address != "") {
			var addressArr = address.split(/;|；/);
			if (addressArr != null && addressArr.length > 0) {
				for (var i = 0; i < addressArr.length; i++) {
					if (addressArr[i] != null && addressArr[i] != '') {
						emailComplemented(self.parent(), addressArr[i]);
					}
				}
			}
			self.val('');
		}
	});
	//点击外层div input获取焦点
	$(".div-input").click(function(event) {
		$(this).find(".e-edit-addr").focus();
		if ($(".div-input .e-edit-block[now-focus='1']")) {
			$(".div-input .e-edit-block[now-focus='1']").attr('now-focus', 0);
		}
		if($(".e-addr-trim[now-focus='1']")) {
			$(".e-addr-trim[now-focus='1']").attr('now-focus', 0);
		}
		$(this).find(".e-edit-block").attr('now-focus', 1);
		event.stopPropagation();
	});
	//点击联系人之间的空格
	$(".div-input").on('click', '.e-addr-trim', function(event) {
		var self = $(this);
		if($(".e-addr-trim[now-focus='1']")) {
			$(".e-addr-trim[now-focus='1']").attr('now-focus', 0);
		}
		if ($(".div-input .e-edit-block[now-focus='1']")) {
			$(".div-input .e-edit-block[now-focus='1']").attr('now-focus', 0);
		}
		self.attr('now-focus', 1);
		self.append('<input class="e-del-ipt" type="text" size="1">');
		self.find(".e-del-ipt").focus();
		event.stopPropagation();
	}).on('blur', '.e-del-ipt', function() {
		var self = $(this);
		var address = self.val().trim();
		if (address != "") {
			var addressArr = address.split(/;|；/);
			if (addressArr != null && addressArr.length > 0) {
				for (var i = 0; i < addressArr.length; i++) {
					if (addressArr[i] != null && addressArr[i] != '') {
						emailComplemented(self.parent(), addressArr[i]);
					}
				}
			}
			self.parent().width(10);
			self.remove();
		} else {
			self.remove();
		}
	}).on('keydown', '.e-del-ipt', function(event) {
		var self = $(this);
		var selfVal = self.val();
		self.width(textWidth(selfVal) + 10);
		self.parent().width(textWidth(selfVal) + 10);
		if (selfVal == "") {
			if (event.keyCode == 8) { //删除键
				if(self.parent().parent().prev().hasClass('e-email-block')){
					self.parent().parent().prev().remove();
				}
			} else if (event.keyCode == 46) { //delete键
				if(self.parent().parent().hasClass('e-email-block')){
					var nextDiv = self.parent().parent().next();
					self.parent().parent().remove();
					if(nextDiv.hasClass('e-email-block')){
						nextDiv.find(".e-addr-trim").click();
					} else if(nextDiv.hasClass('e-edit-block')) {
						nextDiv.find(".nui-editableAddr-ipt").focus();
					}
				}
			}
		}
	});
	//邮件姓名补全及判断
	var emailComplemented = function(ele, address) {
		var emailError = false;
		var emailRule = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;
		if(!emailRule.test(address)) {
			emailError = true;
		}
		var name = '';
		if (externalEmailList[address]) {
			name = externalEmailList[address];
		}
		if (innerEmailList[address]) {
			name = innerEmailList[address];
		}
		if(name == '') {
			name = address;
		}
		ele.before(addrHtmlTmp(name, address, emailError));
	}
	//联系人模板
	var addrHtmlTmp = function(name, email, error) {
		var errorClass = "";
		if(error) {
			errorClass = "nui-addr-error";
		}
		var addrHtml = '<div class="e-email-block" data-e-name="'+name+'" data-e-email="'+email+'" data-e-error="'+error+'"><div class="e-addr-trim"></div>'
			+ '<div class="nui-editableAddr nui-addr nui-addr-hasAdd '+errorClass+'" title="'+email+'" unselectable="on">'
			+ '<strong class="nui-addr-name" unselectable="on">'+name+'</strong>'
			+ '<em class="nui-addr-email" unselectable="on">&lt;'+email+'&gt;</em>'
			+ '</div></div>';
		
		return addrHtml;
	}
	//计算输入文本宽度
	var textWidth = function(text){ 
        var sensor = $('<pre>'+ text +'</pre>').css({display: 'none'}); 
        $('body').append(sensor); 
        var width = sensor.width();
        sensor.remove(); 
        return width + 1;
    };
	
    //外部联系人
	function loadContactsList() {
		using("ztree_exhide",function(){
			var setting = {
				data: {
					simpleData: {
						enable: true
					}
				},
				view: {
					addDiyDom: addDiyDom,
					showIcon : true
				},
				callback: {
					onClick: onClick
				}
				
			};
			ajax.init().success(function(code,msg,data){
				if (code == 0) {
					var zNodes = data;
					if (data.length > 0) {
						data[0].open = true;
					}
					for (var i = 0; i < zNodes.length; i++) {
						for(var j = 0; j < zNodes[i].children.length; j++) {
							externalEmailList[zNodes[i].children[j].contactsMail] = zNodes[i].children[j].name;
						}
					}
					var treeObj = $.fn.zTree.init($("#externalTree"), setting, zNodes);
				}
			}).post(BASEPATH + "/oa/emailContacts/getEmailContactsTree.htm");
			function addDiyDom(treeId, treeNode) {
				var tree = $.fn.zTree.getZTreeObj(treeId).setting.treeObj;
				tree.find("span[id='"+treeNode.tId+"_switch']").remove();
				if(treeNode.level==0){
					tree.find("span[id='"+treeNode.tId+"_ico']").addClass("bgcolor").removeClass("ico_docu");
				}
				
			}
			
			//用于捕获节点被点击的事件回调函数
			function onClick(event, treeId, treeNode, clickFlag) {
				
				//一级节点时 调用展开
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				//$.fn.zTree.getZTreeObj(treeId).setting.treeObj.find("span[id='"+treeNode.tId+"_switch']").remove();
				if(treeNode.open) {
					treeObj.expandNode(treeNode,false);
					return;
				}
				if(treeNode.glevel == 1) {
					if(treeNode.children.length==0){
						$.messager.show({
							title:'',
							msg: '该分组暂无联系人!',
							timeout:1000,
							showType:'slide'
						});
						return;
					}
					treeObj.expandNode(treeNode,true,false,true,false);
					return;
				}
				var hasFocus = false;
				if($(".e-addr-trim[now-focus='1']").length > 0) {
					hasFocus = true;
					var emailList = $(".e-addr-trim[now-focus='1']").parent().parent().find('.e-email-block');;
					checkHasEmail($(".e-addr-trim[now-focus='1']").parent(), emailList, treeNode.name, treeNode.contactsMail);
				}
				if ($(".div-input .e-edit-block[now-focus='1']").length > 0) {
					hasFocus = true;
					var emailList = $(".div-input .e-edit-block[now-focus='1']").parent().find('.e-email-block');
					checkHasEmail($(".div-input .e-edit-block[now-focus='1']"), emailList, treeNode.name, treeNode.contactsMail);
				}
				if (!hasFocus) {
					var emailList = $("#toAddr .e-email-block");
					checkHasEmail($("#toAddr .e-edit-addr").parent(), emailList, treeNode.name, treeNode.contactsMail);
				}
			}
			
		});
	}
	//内部联系人
	function loadInnerContactsList() {
		using("ztree_exhide",function(){
			var setting = {
				view: {
					addDiyDom: addDiyDom,
					showIcon : true,
					showTitle: true
				},
				callback: {
					onClick: onClick
				},
				data: {
					simpleData: {
						enable: true,
						idKey: "id",
						pIdKey: "pid",
						rootPId: 0
					},
					key: {
						title: "name"
					}
				}
			};
			ajax.init().success(function(code,msg,data){
				if (code == 0) {
					var zNodes = data;
//					zNodes[0].pid = 0;
//					zNodes[0].open = true;
					for (var i = 0; i < zNodes.length; i++) {
						if (zNodes[i].type == 'USER') {
							innerEmailList[zNodes[i].email] = zNodes[i].name;
						}
					}
					var treeObj = $.fn.zTree.init($("#innerTree"), setting, zNodes);
				}
			}).post(BASEPATH + "/oa/emailContacts/getInnerContactsTree.htm");
			
			function addDiyDom(treeId, treeNode) {
				var tree = $.fn.zTree.getZTreeObj(treeId).setting.treeObj;
				tree.find("span[id='"+treeNode.tId+"_switch']").remove();
				if(treeNode.level==0){
					tree.find("span[id='"+treeNode.tId+"_ico']").addClass("bgcolor").removeClass("ico_docu");
				}
				
			}
			//用于捕获节点被点击的事件回调函数
			function onClick(event, treeId, treeNode, clickFlag) {
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeNode.open) {
					treeObj.expandNode(treeNode,false);
					return;
				}
				//一级节点时 调用展开
				if(treeNode.type == "DEPT") {
					treeObj.expandNode(treeNode,true,false,true,false);
					return;
				}
				var hasFocus = false;
				if($(".e-addr-trim[now-focus='1']").length > 0) {
					hasFocus = true;
					//判断重复
					var emailList = $(".e-addr-trim[now-focus='1']").parent().parent().find('.e-email-block');;
					checkHasEmail($(".e-addr-trim[now-focus='1']").parent(), emailList, treeNode.name, treeNode.email);
				}
				if ($(".div-input .e-edit-block[now-focus='1']").length > 0) {
					hasFocus = true;
					var emailList = $(".div-input .e-edit-block[now-focus='1']").parent().find('.e-email-block');
					checkHasEmail($(".div-input .e-edit-block[now-focus='1']"), emailList, treeNode.name, treeNode.email);
					
				}
				if (!hasFocus) {
					//判断重复
					var emailList = $("#toAddr .e-email-block");
					checkHasEmail($("#toAddr .e-edit-addr").parent(), emailList, treeNode.name, treeNode.email);
				}
			}
		});
	}
	//先检查input中是否已添加次邮件，没有继续添加
	function checkHasEmail(ele, emailList, name, email) {
		if (emailList.length > 0) {
			var flag = true;
			for (var i = 0; i < emailList.length; i++) {
				if($(emailList[i]).data('e-email') == email) {
					flag = false;
					break;
				}
			}
			if (flag) {
				ele.before(addrHtmlTmp(name, email, false));
			}
		} else {
			ele.before(addrHtmlTmp(name, email, false));
		}
	}
	//添加邮件账号
	function addEmail(){
		$('#addEmaildlg').dialog({
			iconCls: 'icon-save',
			title:'添加邮箱账号',
		    height: 260,
			href: BASEPATH + '/view/oa/email/addEmail',
			onLoad: function() {
				$("#emailSuffix").text(emailSuffix);
			},
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#addEmailForm").form("validate")){
							//验证邮箱是否重复
							var email = $("#email").textbox("getValue");
							email += emailSuffix;
							ajax.init().success(function(code, msg, data){
								if (code == 0) {
									ajax.init().success(function(code, msg, data){
										if (code == 0) {
											loadContactsList();
											loadInnerContactsList()
											$("#addEmaildlg").dialog("close");
											$.messager.show({
												title:'',
												msg: msg,
												timeout:1000,
												showType:'slide'
											});
										}
									}).error(function(code, msg, data){
										$.messager.show({
											title:'',
											msg: msg,
											timeout:1000,
											showType:'slide'
										});
									}).post(BASEPATH+'/oa/email/saveUserMail.htm', {email: email});
								}
							}).error(function(code, msg, data) {
								$.messager.show({
									title:'',
									msg: msg,
									timeout:1000,
									showType:'slide'
								});
							}).post(BASEPATH+'/oa/email/checkUserMailRepeat.htm', {email: email});
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#addEmaildlg').dialog('close');
				}
			}]
		});
		$("#addEmaildlg").dialog("open");
	}
	
	$('.send').click(function() {
		if (sendType == 'forward') {
			createFromData("/oa/email/forwardEmail.htm", 'send');
		} else if (sendType == "reply") {
			createFromData("/oa/email/replyEmail.htm", 'send');
		} else if (sendType == 'draftSend') {//草稿箱邮件再次发送
			createFromData("/oa/email/draftSendEmail.htm", 'send');
		} else {
			createFromData("/oa/email/sendEmail.htm", 'send');
		}
	});
	$(".draft").click(function() {
		clearFrom = false;
		if (sendType == 'draftSend') {//草稿箱再次保存
			createFromData("/oa/email/saveDraftAgainEmail.htm", 'draft');
		} else if (sendType == 'reply' || sendType == 'forward') { //转发或回复邮件保存草稿邮件
			createFromData("/oa/email/saveReplyForwardDraftEmail.htm", 'draft');
		} else {
			createFromData("/oa/email/saveDraftEmail.htm", 'draft');
		}
	});
	using(["file_upload"],function(){
		var maxFileSize = 20; //5 MB
		$(".fileupload").fileupload({
			url: BASEPATH + "/oa/email/uploadAttachment.htm",
			dataType: 'json',
			autoUpload: true,
			acceptFileTypes: /(\.[a-zA-Z0-9]+)$/i,///(\.|\/)(rar|doc|docx|zip|pdf|txt|swf|wmv|gif|png|jpg|jpeg|bmp)$/i
			maxFileSize: 1024 * 1024 * maxFileSize,
	        start: function (e, data) {//上传前
		       	$(".file-item-"+fileItem+" .file-finish-bar-block").show();
		       	$(".file-item-"+fileItem+" .file-finish-bar-result").show();
		    },
			progressall: function (e, data) {
	            var progress = parseInt(data.loaded / data.total * 100, 10);
	            $(".file-item-"+fileItem+" .file-finish-bar-result").text(progress + '%');
	            $(".file-item-"+fileItem+" .file-finish-bar").css('width',progress + '%');
	       }
		 }).bind('fileuploadprocessalways', function (e, data) {
	    	 var error = data.files[0].error;
		   	 if (error == 'File type not allowed') {
		   		 showErrorMessage('格式不正确');
		   	 } else if (error == 'File is too large') {
		   		 showErrorMessage('文件太大，请不要超过' + maxFileSize + 'M,请重新上传');
		   	 } else {
		   		 var html='<div class="file-new-list file-finish-block file-item-'+fileItem+'">'
		   				+ '<div class="file-finish-name">'+data.files[0].name+'</div><div class="file-finish-del-block">'
		   		 + '<a href="javascript:void(0)" class="file-finish-del">删除</a></div>'
		   		 + '<div class="file-finish-result">'+bytesToSize(data.files[0].size)+'<span class="file-finish-result-msg">上传完成</span></div>'
		   		 + '<div class="file-finish-bar-block"><div class="file-finish-bar"></div></div>'
		   		 + '<div class="file-finish-bar-result">0%</div>'
		   		 + '<input type="hidden" class="filePath" value=""></div>'
		   		 $("#fileFinish").append(html);
		   	}
		 }).bind('fileuploaddone', function (e, data) { //上传完成
		   	 if (data.result.code == 0) {
		   		 $(".file-item-"+fileItem+" .file-finish-bar-result").remove();
		         $(".file-item-"+fileItem+" .file-finish-bar").remove();
		         $(".file-item-"+fileItem+" .file-finish-result").show();
		         $(".file-item-"+fileItem+" .file-finish-bar-block").remove();
		         $(".file-item-"+fileItem+" .file-finish-del-block").show();
		         $(".file-item-"+fileItem+" .filePath").val(data.result.data.returnFilePath);
		   	 } else {
		   		 $(".file-item-"+fileItem+" .file-finish-bar-result").remove();
		   		 $(".file-item-"+fileItem+" .file-finish-bar").remove();
		   		 $(".file-item-"+fileItem+" .file-finish-bar-block").remove();
		   		 $(".file-item-"+fileItem+" .filePath").remove();
		   		 $(".file-item-"+fileItem+" .file-finish-result-msg").text('上传失败').css('color', 'red');
		   		 $(".file-item-"+fileItem+" .file-finish-del-block").show();
		   		 $(".file-item-"+fileItem+" .file-finish-result").show();
		   	 }
		   	fileItem += 1;
		});
	});
	
	module.exports = {
		init: function() {
			pid = this.pid||urltool.getParam("pid");
			ajax.init().success(function(code,msg,data){
				if(code==0){
					loadContactsList();
					loadInnerContactsList()
				} 
			}).error(function(code,msg,data) {
				$.messager.confirm('系统提示', msg, function(r){
		             if (r){
		            	 addEmail();
		             }
		        });
			}).post(BASEPATH + "/oa/email/checkUserMailExist.htm");
			
		}
	}
});
